<template>
  <div>
    <TopFilter ref="formPanel" :list="formList" :cols="3" :labelWidth="90" @filterChange="searchHealthHandle"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :datakey="'rows'"
      :params="fetchParams"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="SelectChange"
      :height="250"
    >
      <template slot="controls">
        <multiuse-button size="small" type="primary" :auth-list="auths" auth-mark="/repair/archivesManage/healthQuery/Allowresetreport" @click="upload">上报</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="detailedVisible" title="明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detailed :rowData="rowData" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { searchHealthHandle, upload } from '@/api/repair/archivesManage/healthQuery/index';
import detailed from './detailed';

export default {
  name: 'healthquery',
  components: {
    detailed
  },
  mixins: [dictionary, authority],
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      fetchApi: searchHealthHandle,
      fetchParams: { xhrAbort: true }, // 调用数据
      columns: this.createTablelist(),
      detailedVisible: false,
      rowData: {},
      items: [] // 定义选中行ID
    };
  },
  mounted() {
    this.saveData = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'SELECT',
          label: '上报状态',
          fieldName: 'status',
          itemList: this.createDictList('8091')
        },
        {
          type: 'RANGE_DATE',
          dateType: 'exactdaterange',
          label: '结算日期',
          style: { minWidth: '200px' },
          fieldName: 'begin|end'
        }
      ];
    },
    createTablelist() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,

          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true
        },
        {
          title: 'VIN',
          dataIndex: 'VIN',
          width: 150,
          sorter: true
        },
        {
          title: '送修日期',
          dataIndex: 'RO_CREATE_DATE',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '送修里程',
          dataIndex: 'IN_MILEAGE',
          sorter: true
        },
        {
          title: '故障描述',
          dataIndex: 'RO_TROUBLE_DESC',
          width: 150,
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '结算日期',
          dataIndex: 'BALANCE_TIME',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          width: 120,
          sorter: true
        },

        {
          title: '结算单号',
          dataIndex: 'BALANCE_NO',
          sorter: true
        },

        {
          title: '上报状态',
          dataIndex: 'UPLOAD_STATUS',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.UPLOAD_STATUS, '8091')}</span>;
          }
        },

        {
          title: '上报日期',
          dataIndex: 'UPLOAD_TIME',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },

        {
          title: '备注原因',
          dataIndex: 'REMARK',
          sorter: true
        }
      ];
    },
    searchHealthHandle(val) {
      const params = Object.assign({}, val, { xhrAbort: false });
      console.log('健康记录查询', params);
      this.fetchParams = params;
    },
    detailHandle(row) {
      this.rowData = row;
      this.detailedVisible = true;
    },
    // 选中行
    SelectChange(row) {
      this.items = [];
      console.log('row', row);
      for (let i = 0; i < row.length; i++) {
        this.items.push(row[i].ITEM_ID);
      }
    },
    async upload() {
      if (this.items.length == 0) {
        return this.$notify.waring({ title: '提示信息', message: '请选择至少一条数据上报' });
      }

      let itemIdList = {
        itemIdList: this.items.join()
      };

      let res = await upload(itemIdList);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '上报成功' });
        // 刷新数据显示状态
        this.fetchParams = { ...this.fetchParams };
      } else {
        return this.$notify.error({ title: '提示信息', message: res.errMsg });
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
